/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package image.edge;

import image.edge.zerocross.ZeroCross;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.Set;
import mask.ImageMask;

/**
 *
 * @author tommy
 */
public class ZeroCrossEdgeDetection extends EdgeDetectorImpl {
	private ImageMask laplacianMask;	
	private Double threshold;
	private static double[][] laplacianOperator = {
		  { -0d, -1d, 0d },
		  { -1d, 4d, -1d },
		  { -0d, -1d, 0d },
		};
	
	public ZeroCrossEdgeDetection (BufferedImage src) {
		super(src);
		this.laplacianMask = new ImageMask(laplacianOperator, 3);
		this.threshold = 0d;
	}
	
	public ZeroCrossEdgeDetection (BufferedImage src, double threshold) {
		this(src);
		this.threshold = threshold;
	}
	
	public BufferedImage FindEdges() {
		Double[] laplacianMagnitudes;
		Set<Integer[]> crossIndexes;
		ZeroCross cross;
		
		WritableRaster outRaster;

		// Only intensity (grey-scale) images supported for the time being
		if (src.getType() != BufferedImage.TYPE_BYTE_GRAY) {
			return null;
		}
		
		// get original image pixels
		outRaster = ret.getRaster();
		
		// Applies laplacian Operator to find zero crossings in the second order derivative
		// does not process padded border edge
		laplacianMagnitudes = laplacianMask.applyMask(src);
		cross = new ZeroCross(ret, threshold);
		crossIndexes = cross.findCrossings(laplacianMagnitudes);
		
		
		for (Integer[] crossIndex : crossIndexes) {
			outRaster.setSample(crossIndex[0], crossIndex[1], 0, 255);
		}
		
		return ret;
	}
}
